package com.ruoyi.generator.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.generator.domain.vo.AppPageTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.generator.mapper.FromBuildPageMapper;
import com.ruoyi.generator.domain.FromBuildPage;
import com.ruoyi.generator.service.IFromBuildPageService;

/**
 * 移动端页面树Service业务层处理
 *
 * @author ZGL
 * @date 2024-12-12
 */
@Service
public class FromBuildPageServiceImpl implements IFromBuildPageService
{
    @Autowired
    private FromBuildPageMapper fromBuildPageMapper;

    /**
     * 查询移动端页面树
     *
     * @param id 移动端页面树主键
     * @return 移动端页面树
     */
    @Override
    public FromBuildPage selectFromBuildPageById(Long id)
    {
        return fromBuildPageMapper.selectFromBuildPageById(id);
    }

    /**
     * 查询移动端页面树列表
     *
     * @param fromBuildPage 移动端页面树
     * @return 移动端页面树
     */
    @Override
    public List<FromBuildPage> selectFromBuildPageList(FromBuildPage fromBuildPage)
    {
        return fromBuildPageMapper.selectFromBuildPageList(fromBuildPage);
    }

    /**
     * 新增移动端页面树
     *
     * @param fromBuildPage 移动端页面树
     * @return 结果
     */
    @Override
    public int insertFromBuildPage(FromBuildPage fromBuildPage)
    {
        fromBuildPage.setCreateTime(DateUtils.getNowDate());
        fromBuildPage.setBuildKey(UUID.fastUUID().toString());
        return fromBuildPageMapper.insertFromBuildPage(fromBuildPage);
    }

    /**
     * 修改移动端页面树
     *
     * @param fromBuildPage 移动端页面树
     * @return 结果
     */
    @Override
    public int updateFromBuildPage(FromBuildPage fromBuildPage)
    {
        fromBuildPage.setUpdateTime(DateUtils.getNowDate());
        FromBuildPage _fromBuildPage = new FromBuildPage();
        _fromBuildPage.setParentId(fromBuildPage.getParentId());
        _fromBuildPage.setNum(fromBuildPage.getNum());
        List<FromBuildPage> fromBuildPages = fromBuildPageMapper.selectFromBuildPageList(_fromBuildPage);
        long num = 0;
        for (FromBuildPage buildPage : fromBuildPages) {
            if(buildPage.getNum() == null){
                buildPage.setNum(num + 1);
                num= num + 1;
            }else{
                buildPage.setNum(buildPage.getNum() + 1);
            }
            fromBuildPageMapper.updateFromBuildPage(buildPage);
        }
        return fromBuildPageMapper.updateFromBuildPage(fromBuildPage);
    }

    /**
     * 修改页面名称
     * @param fromBuildPage 移动端页面树
     * @return
     */
    @Override
    public int updatePageName(FromBuildPage fromBuildPage) {
        return fromBuildPageMapper.updateFromBuildPage(fromBuildPage);
    }

    /**
     * 批量删除移动端页面树
     *
     * @param ids 需要删除的移动端页面树主键
     * @return 结果
     */
    @Override
    public int deleteFromBuildPageByIds(Long[] ids)
    {
        return fromBuildPageMapper.deleteFromBuildPageByIds(ids);
    }

    /**
     * 删除移动端页面树信息
     *
     * @param id 移动端页面树主键
     * @return 结果
     */
    @Override
    public int deleteFromBuildPageById(Long id)
    {
        return fromBuildPageMapper.deleteFromBuildPageById(id);
    }

    /**
     * 移动端页面树树结构信息
     * @return
     */
    @Override
    public AppPageTree getFromBuildPageTree(String appKey) {
        FromBuildPage fromBuildPage = new FromBuildPage();
        fromBuildPage.setAppKey(appKey);
        fromBuildPage.setStatus("0");
        List<FromBuildPage> fromBuildPages = fromBuildPageMapper.selectFromBuildPageList(fromBuildPage);
        //节点信息
        AppPageTree appPageTree = new AppPageTree();
        Map<String, List<FromBuildPage>> groupPage = fromBuildPages.stream()
                .collect(Collectors.groupingBy(FromBuildPage::getParentId));
        fromBuildPages.stream().filter(item -> item.getParentId().equals("0")).forEach(item -> {
            appPageTree.setId(item.getId());
            appPageTree.setParentId(item.getParentId());
            appPageTree.setPageName(item.getPageName());
            appPageTree.setBuildKey(item.getBuildKey());
            appPageTree.setInitMethod(item.getInitMethod());
            appPageTree.setDestroyMethod(item.getDestroyMethod());
            appPageTree.setNum(item.getNum());
            appPageTree.setLabel(item.getPageName());
            appPageTree.setCode(item.getBuildKey());
            return;
        });
        getAppPageTree(appPageTree.getId().toString(), groupPage,appPageTree);
        return appPageTree;
    }

    /**
     * 返回移动端页面树
     * @param parentId
     * @param groupPage
     * @param appPageTree
     * @return
     */
    private AppPageTree getAppPageTree(String parentId ,Map<String, List<FromBuildPage>> groupPage,AppPageTree appPageTree){
        //子节点
        ArrayList<AppPageTree> children = new ArrayList<>();
        AppPageTree childrenInfo = null;
        List<FromBuildPage> fromBuildPages = groupPage.get(parentId);
        if(fromBuildPages == null){
            appPageTree.setChildren(children);
            return appPageTree;
        }
        for (FromBuildPage fromBuildPage : fromBuildPages) {
            if(fromBuildPage.getParentId().equals(parentId)){
                childrenInfo = new AppPageTree();
                childrenInfo.setId(fromBuildPage.getId());
                childrenInfo.setParentId(fromBuildPage.getParentId());
                childrenInfo.setPageName(fromBuildPage.getPageName());
                childrenInfo.setBuildKey(fromBuildPage.getBuildKey());
                childrenInfo.setNum(fromBuildPage.getNum());
                childrenInfo.setInitMethod(fromBuildPage.getInitMethod());
                childrenInfo.setDestroyMethod(fromBuildPage.getDestroyMethod());
                childrenInfo.setLabel(fromBuildPage.getPageName());
                childrenInfo.setCode(fromBuildPage.getBuildKey());
                //递归获取子节点的信息
                getAppPageTree(fromBuildPage.getId().toString(), groupPage, childrenInfo);
                //缓存树
                children.add(childrenInfo);
            }
        }
        //递归获取子节点的信息
        appPageTree.setChildren(children);
        return appPageTree;
    }
}
